// !@#$McCaBe-InStRuMeNtAtIoN$#@!
// Instrumented with the McCabe Java Parser
// Version: 8.1.0.1 - 20110309
// Using Path Vector Instrumentation
// Date: Sun Apr 01 2012 20:37:35
package Driver;

import EventManager.CreateBookingRequest;
import EventManager.RequestProcessor;
import EventManager.RequestStatus;
import Model.Event;
import Model.Person;
import Model.Seat;
import Service.OutputManager;

import com.mccabe.*;


class AsyncBot_147b479c8152f511a16379fdfbae0749 {
    static java.lang.Object info[];
    static {
        info = new java.lang.Object[2];
        info[0] = new Integer(-1);
        info[1] = new String("R:\\yu\\Concordia\\SOEN 344 Design II\\Term Assignment\\Code Repo\\trunk\\Iteration 3\\ERS\\src\\Driver\\AsyncBot.java");
    }
}


/**
 * This class impersonates a person and attempts to reserve as many seats as possible for all events until seats run out.
 * @author Yury Kitaev
 *
 */
public class AsyncBot extends Thread 
{
	private Person person;
	
	
	public AsyncBot(Person p)
	{
int _mcFreq_602e7d0d1b1829c90eb930efc2669593[] = new int[1];
PathVec_ERS_Iter3.enterMethod(AsyncBot_147b479c8152f511a16379fdfbae0749.info,0,_mcFreq_602e7d0d1b1829c90eb930efc2669593,0);

		person = p;
	}
	
	@Override
	public void run()
	{
int _mcFreq_bf83b7d7324190a62a7df5e319cc43d8[] = new int[1];
PathVec_ERS_Iter3.enterMethod(AsyncBot_147b479c8152f511a16379fdfbae0749.info,1,_mcFreq_bf83b7d7324190a62a7df5e319cc43d8,0);

		bookAllSeats();
	}
	
	/**
	 * Will attempt to reserve as much seats as possible (calling yield in unexpected places just to be fair and to test the system concorrency handling skills)
	 */
	protected void bookAllSeats()
	{
int _mcFreq_e833f8d8e4549b4a7c9ce4fc25e42168[] = new int[15];
PathVec_ERS_Iter3.enterMethod(AsyncBot_147b479c8152f511a16379fdfbae0749.info,2,_mcFreq_e833f8d8e4549b4a7c9ce4fc25e42168,0);

		for (int i = 0; 
PathVec_ERS_Iter3.decision(AsyncBot_147b479c8152f511a16379fdfbae0749.info,2,_mcFreq_e833f8d8e4549b4a7c9ce4fc25e42168,1,2,
PathVec_ERS_Iter3.testCondition(i < Facade.getCatalog().getEvents().size()
))
; ++i)
		{
			Event e =  Facade.getCatalog().getEvents().get(i);
			OutputManager.println(person.getFirstName() + " will try to register for event " + e.getSpecification().getTitle()  + "(ID " + e.getId() + ")");
			int seat = 0;
			// find a seat
			do 
			{
				seat = 0;
				for (int j = 0; 
PathVec_ERS_Iter3.decision(AsyncBot_147b479c8152f511a16379fdfbae0749.info,2,_mcFreq_e833f8d8e4549b4a7c9ce4fc25e42168,3,4,
PathVec_ERS_Iter3.testCondition(j < e.getVenue().getSeats().size()
))
; ++j)
				{
					Seat s = e.getVenue().getSeats().get(j);
					if (
PathVec_ERS_Iter3.decision(AsyncBot_147b479c8152f511a16379fdfbae0749.info,2,_mcFreq_e833f8d8e4549b4a7c9ce4fc25e42168,5,6,
PathVec_ERS_Iter3.testCondition(!s.isTaken()
))
)
					{
						seat = s.getNumber();
					}
				}
				
				if (
PathVec_ERS_Iter3.decision(AsyncBot_147b479c8152f511a16379fdfbae0749.info,2,_mcFreq_e833f8d8e4549b4a7c9ce4fc25e42168,7,8,
PathVec_ERS_Iter3.testCondition(seat > 0
))
)
				{
					OutputManager.println(person.getFirstName() + " is trying to get seat " + seat + " for " + e.getSpecification().getTitle()  + "(ID " + e.getId() + ")");
					
					yield(); // Done on purpose to allow someone to grab the seat
					
					CreateBookingRequest r = new CreateBookingRequest(person, e.getId(), seat);
				
					RequestProcessor.process(r);
					
					if (
PathVec_ERS_Iter3.decision(AsyncBot_147b479c8152f511a16379fdfbae0749.info,2,_mcFreq_e833f8d8e4549b4a7c9ce4fc25e42168,9,10,
PathVec_ERS_Iter3.testCondition(RequestStatus.Sustained == r.getStatus()
))
)
					{
						if (
PathVec_ERS_Iter3.decision(AsyncBot_147b479c8152f511a16379fdfbae0749.info,2,_mcFreq_e833f8d8e4549b4a7c9ce4fc25e42168,11,12,
PathVec_ERS_Iter3.testCondition(seat == r.getBooking().getSeat().getNumber()
))
)
						{
							OutputManager.println(person.getFirstName() + " made reservation for " + e.getSpecification().getTitle() + "(ID " + e.getId() + ")" + " for the seat s/he wanted (lucky!)");
						}
						else
						{
							OutputManager.println(person.getFirstName() + " made reservation for " + e.getSpecification().getTitle() + "(ID " + e.getId() + ")" + ", but for a different seat (" + r.getBooking().getSeat().getNumber() + ")");
						}
					}
					else
					{
						OutputManager.println(person.getFirstName() + " failed to get seat " + seat + " - someone got it first, and there are no more left for " +  e.getSpecification().getTitle() + "(ID " + e.getId() + ")");
					}
				}
				yield();
			} while (
PathVec_ERS_Iter3.decision(AsyncBot_147b479c8152f511a16379fdfbae0749.info,2,_mcFreq_e833f8d8e4549b4a7c9ce4fc25e42168,13,14,
PathVec_ERS_Iter3.testCondition(seat > 0
))
); // loop until seats run out for the event
			yield();
			
		} // loop until events run out
		OutputManager.println(person.getFirstName() + " can find no more events and leaves.");
	}
}
